package bi.uc.client;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.bi_uc.www.R;

import bi.uc.login.Info;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class QuickpayActivity extends Activity implements OnClickListener,
		OnTouchListener, OnFocusChangeListener, Runnable {
	EditText cardNum;
	Info info = Info.getInfo();
	TextView ownmoney;
	TextView ownscore;
	ImageButton searchCardNum;
	EditText needPay;
	TextView discountPay;
	TextView payMoney;
	TextView result;
	EditText money;
	EditText score;
	TextView scoreMoney;
	EditText cash;
	EditText password;
	
	TextWatcher needPayWatcher;
	TextWatcher scoreWatcher;
	TextWatcher moneyWatcher;
	TextWatcher cashWatcher;
	
	TableRow ownmoneyrow;
	TableRow moneyrow;
	TableRow ownscorerow;
	TableRow scorerow;
	TableRow scoremoneyrow;
	TableRow passwordrow;
	TableRow cashrow;
	
	double dismoney;	//折后价格
	double needmoney = 0;	//应付金额
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		((ExitApplication)getApplication()).addActivity(this);
		setContentView(R.layout.quickpay);
		ownmoneyrow = (TableRow) findViewById(R.id.ownmoneyrow);
		moneyrow = (TableRow) findViewById(R.id.moneyrow);
		ownscorerow = (TableRow) findViewById(R.id.ownscorerow);
		scorerow = (TableRow) findViewById(R.id.scorerow);
		scoremoneyrow = (TableRow) findViewById(R.id.scoremoneyrow);
		passwordrow = (TableRow) findViewById(R.id.passwordrow);
		cashrow = (TableRow) findViewById(R.id.cashrow);
		cardNum = (EditText) findViewById(R.id.cardNum);
		ownmoney = (TextView) findViewById(R.id.ownmoney);
		ownscore = (TextView) findViewById(R.id.ownscore);
		needPay = (EditText) findViewById(R.id.needpay);
		payMoney = (TextView) findViewById(R.id.paymoney);
		result = (TextView) findViewById(R.id.result);
		money = (EditText) findViewById(R.id.money);
		score = (EditText) findViewById(R.id.score);
		scoreMoney = (TextView) findViewById(R.id.scoremoney);
		cash = (EditText) findViewById(R.id.cash);
		password = (EditText) findViewById(R.id.password);
		discountPay = (TextView) findViewById(R.id.discountPay);
		initTextWatcher();
		if (!info.isCardQuickPay) {
			ownmoneyrow.setVisibility(View.GONE);
			moneyrow.setVisibility(View.GONE);
			ownscorerow.setVisibility(View.GONE);
			scorerow.setVisibility(View.GONE);
			scoremoneyrow.setVisibility(View.GONE);
			passwordrow.setVisibility(View.GONE);
		} else {
			money.setOnFocusChangeListener(this);
			score.setOnFocusChangeListener(this);
			score.setOnTouchListener(new OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					if (info.card.isScorePay ) {					
						score.setFocusableInTouchMode(true);
					} else {
						Toast.makeText(QuickpayActivity.this, "该卡未启用积分抵现！", Toast.LENGTH_SHORT).show();
						score.setFocusableInTouchMode(false);
					}
					return false;
				}
			});
			password.setOnTouchListener(new OnTouchListener() {
				
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					// TODO Auto-generated method stub
					if (info.isPayNeedPassword && (Math.abs(Double.parseDouble(money.getText().toString()))>0.0000001 || !score.getText().toString().equals("0"))) {					
						password.setFocusableInTouchMode(true);
					} else {
						password.setFocusableInTouchMode(false);
					}
					return false;
				}
			});
		}
		
		if (!info.isCashQuickPay) {
			Log.d("****", "*****");
			cashrow.setVisibility(View.GONE);
		} else {
			cash.setOnFocusChangeListener(this);
		}
		if (info.isExist(info.card.cardNum)) {
			cardNum.setText(info.card.cardNum);
			ownmoney.setText(info.subZeroAndDot(info.card.money+"") + "元");
			ownscore.setText(info.card.score + "枚");
			
			needPay.setEnabled(true);			
			money.setEnabled(true);
			score.setEnabled(true);			
			cash.setEnabled(true);
		}		
		searchCardNum = (ImageButton) findViewById(R.id.searchbtn);
		searchCardNum.setOnClickListener(this);
		searchCardNum.setOnTouchListener(this);
		needPay.setOnFocusChangeListener(this);		
	}

	/**
	 * 
	 */
	private void initTextWatcher() {
		// TODO Auto-generated method stub
		needPayWatcher = new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start,
					int before, int count) {
				// TODO Auto-generated method stub
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start,
					int count, int after) {
				// TODO Auto-generated method stub
			}

			@Override
			public void afterTextChanged(Editable s) {
				Log.e("needpay", "结果：" + s.toString());
				if (s.length() > 1 && s.charAt(1) != '.'
						&& s.charAt(0) == '0') {
					needPay.setText(s.toString().substring(1));
					info.setSpan(needPay);
					return;
				}
				Log.e("111","111");
				if (".".equals(s.toString())) {
					needPay.setText("0.");
					info.setSpan(needPay);
					return;
				}
				Log.e("2222","2222");
				if (s.length()>0) {
					if (!info.isCardQuickPay) {
						Log.e("333","333");
						return;
					}
					needmoney = Double.parseDouble(s.toString());
					dismoney = needmoney*info.storeDiscount*info.card.Discount;
					Log.e(String.format("%.2f元", dismoney), "00000");
					discountPay.setText(String.format("%.2f元", dismoney));
					if (info.card.money < dismoney) {
						
						// 需要继续支付的金额
						double shortMoney = dismoney-info.card.money;
						
						if (info.card.isScorePay && shortMoney > 0) {
							// 积分抵现的限额
							double canExchangeMoney = dismoney*info.card.scoreMoneyRatio;
							// 可以兑换的金额上限
							double scoreMaxMoney = info.card.score/info.card.scoreMoneyExchange;
							if (canExchangeMoney >= shortMoney
									&& scoreMaxMoney >= shortMoney) {
								score.setText(""+(int)(shortMoney/info.card.scoreMoneyExchange));
								cash.setText("0");
								shortMoney = 0;
							} else {
								if ((canExchangeMoney >= shortMoney && scoreMaxMoney < shortMoney)
										|| (canExchangeMoney < shortMoney && scoreMaxMoney <= canExchangeMoney)) {
									score.setText(String.valueOf(info.card.score));
									
								} else if (canExchangeMoney < shortMoney
										&& scoreMaxMoney > canExchangeMoney) {
									score.setText(""+(int)(canExchangeMoney/info.card.scoreMoneyExchange));
								}
//								cash.setText(String.format("%.2f",shortMoney-Integer.parseInt(score.getText().toString())/info.card.scoreMoneyExchange));
								shortMoney -= Integer.parseInt(score.getText().toString())/info.card.scoreMoneyExchange;
							}
							double exchangeMoney = Math.round(100*Double.parseDouble(score.getText().toString())/info.card.scoreMoneyExchange)/100.0;
							scoreMoney.setText(info.subZeroAndDot(exchangeMoney+"")+"元");
						} else {
							score.setText("0");
							scoreMoney.setText("0元");
//							cash.setText(String.format("%.2f",shortMoney));
						}
						shortMoney -= Double.parseDouble(cash.getText().toString());
						money.setText(info.subZeroAndDot(info.card.money+""));
//						result.setText("结清");						
						payMoney.setText(info.subZeroAndDot(Math.round((dismoney-shortMoney)*100)/100.0+"")+"元");
						if (shortMoney>0.0000001) {
							result.setText(info.subZeroAndDot("少付"+Math.round(shortMoney*100)/100.0)+"元");
						} else if (Math.abs(shortMoney)<0.0000001) {
							result.setText("结清");
						} else {
							result.setText("找零"+info.subZeroAndDot(Math.round(-shortMoney*100)/100.0+"")+"元");
						}
						
					}else {
						needmoney = Double.parseDouble(s.toString());
						dismoney = needmoney*info.storeDiscount*info.card.Discount;
						String disMoneyString = String.format("%.2f元", dismoney);
						discountPay.setText(disMoneyString);
						
						money.setText(info.subZeroAndDot(""+dismoney));
						result.setText("结清");
						scoreMoney.setText("0元");
						payMoney.setText(info.subZeroAndDot(""+dismoney)+"元");
						score.setText("0");
						cash.setText("0");
					}
				} else {
						needPay.setText("0");
						discountPay.setText("0元");
						info.setSpan(needPay);
						money.setText("0");
						scoreMoney.setText("0元");
						payMoney.setText("0元");
						result.setText("");
						score.setText("0");
						cash.setText("0");
				}
			}
		};
		
		moneyWatcher = new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				Log.d("money", "结果：" + s.toString());
				if (s.length() > 1 && s.charAt(1) != '.'
						&& s.charAt(0) == '0') {
					money.setText(s.toString().substring(1));
					info.setSpan(money);
					return;
				}
				if (s.length() < 1) {
					money.setText("0");
					info.setSpan(money);
					return;
				}
				if (".".equals(s.toString())) {
					money.setText("0.");
					info.setSpan(money);
					return;
				}
				double cardPayMoney = Double.parseDouble(s.toString());
//				double needmoney = Double.parseDouble(needPay.getText().toString());
				if (cardPayMoney > info.card.money) {
					money.setText(info.subZeroAndDot(info.card.money+""));
					info.setSpan(money);
					return;
				}
				if (cardPayMoney > dismoney) {
					money.setText(""+dismoney);
					info.setSpan(money);
					return;
				} else {
					// 需要继续支付的金额
					double shortMoney = dismoney-cardPayMoney;
					
					if (info.card.isScorePay && shortMoney > 0) {
						// 积分抵现的限额
						double canExchangeMoney = dismoney*info.card.scoreMoneyRatio;
						double scoreMaxMoney = info.card.score/info.card.scoreMoneyExchange;
						
						if (canExchangeMoney >= shortMoney
								&& scoreMaxMoney >= shortMoney) {
							score.setText(""+(int)(shortMoney/info.card.scoreMoneyExchange));
							cash.setText("0");
							shortMoney = 0;
						} else {
							if ((canExchangeMoney >= shortMoney && scoreMaxMoney < shortMoney)
									|| (canExchangeMoney < shortMoney && scoreMaxMoney <= canExchangeMoney)) {
								score.setText(""+ info.card.score);
							} else if (canExchangeMoney < shortMoney
									&& scoreMaxMoney > canExchangeMoney) {
								score.setText(""+(int)(canExchangeMoney
												/info.card.scoreMoneyExchange));
							}
//							cash.setText(String.format("%.2f",shortMoney-Integer.parseInt(score.getText().toString())/info.card.scoreMoneyExchange));
							shortMoney -= Integer.parseInt(score.getText().toString())/info.card.scoreMoneyExchange;
						}
						double exchangeMoney = Math.round(Integer.parseInt(score.getText().toString())*100/info.card.scoreMoneyExchange)/100.0;
						scoreMoney.setText(info.subZeroAndDot(exchangeMoney+"")+"元");
					} else {
						score.setText("0");
						scoreMoney.setText("0元");
//						cash.setText(String.format("%.2f",shortMoney));
					}
//					result.setText("结清");
					shortMoney -= Double.parseDouble(cash.getText().toString());
					payMoney.setText(info.subZeroAndDot(Math.round(100*(dismoney-shortMoney))/100.0+"")+"元");
					if (shortMoney>0.0000001) {
						result.setText("少付"+info.subZeroAndDot(Math.round(100*shortMoney)/100.0+"")+"元");
					} else if (Math.abs(shortMoney)<0.0000001) {
						result.setText("结清");
					} else {
						result.setText("找零"+info.subZeroAndDot(Math.round(-100*shortMoney)/100.0+"")+"元");
					}
				}
			}
		};
		
		scoreWatcher = new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if (s.length() > 1 && s.charAt(1) != '.'
						&& s.charAt(0) == '0') {
					score.setText(s.toString().substring(1));
					info.setSpan(score);
					return;
				}
				if (s.length() < 1) {
					score.setText("0");
					info.setSpan(score);
					return;
				}
				
//				double needmoney = Double.parseDouble(needPay.getText().toString());
				
				double cardPayMoney = Double.parseDouble(money.getText().toString());
				// 需要继续支付的金额
				double shortMoney = dismoney-cardPayMoney;
				if ("0".equals(s.toString())) {
					scoreMoney.setText("0元");
//					cash.setText(String.format("%.2f", shortMoney));
//					result.setText("结清");
					shortMoney -= Double.parseDouble(cash.getText().toString());
					if (shortMoney>0.0000001) {
						result.setText("少付"+info.subZeroAndDot(shortMoney+"")+"元");
					} else if (Math.abs(shortMoney)<0.0000001) {
						result.setText("结清");
					} else {
						result.setText("找零"+info.subZeroAndDot(-shortMoney+"")+"元");
					}
					payMoney.setText(info.subZeroAndDot(cardPayMoney+"")+"元");
					return;
				}
								
				// 允许兑换的金额上限
				double scoreMaxMoney = info.card.score/info.card.scoreMoneyExchange;				
				// 积分抵现可以兑换的最大金额
				double canExchangeMoney = dismoney*info.card.scoreMoneyRatio;				
				double exchangeMoney = Integer.parseInt(s.toString())/info.card.scoreMoneyExchange;				
				if (shortMoney>=0) {
					if (exchangeMoney<=scoreMaxMoney && exchangeMoney<=canExchangeMoney && exchangeMoney <= shortMoney) {
//						cash.setText(String.format("%.2f", shortMoney-exchangeMoney));
						shortMoney -= exchangeMoney + Double.parseDouble(cash.getText().toString());
						
						scoreMoney.setText(Math.round(100*exchangeMoney)/100.0+"元");
//						result.setText("结清");
//						payMoney.setText(shortMoney+"元");
						payMoney.setText(Math.round(100*(dismoney-shortMoney))/100.0+"元");
						if (shortMoney>0.0000001) {
							result.setText("少付"+Math.round(100*shortMoney)/100.0+"元");
						} else if (Math.abs(shortMoney)<0.0000001) {
							result.setText("结清");
						} else {
							result.setText("找零"+Math.round(-100*shortMoney)/100.0+"元");
						}
						return;
					} else if (exchangeMoney>scoreMaxMoney && exchangeMoney<=canExchangeMoney && exchangeMoney <= shortMoney) {
						score.setText(""+(int)(scoreMaxMoney/info.card.scoreMoneyExchange));
					} else if (exchangeMoney<=scoreMaxMoney && exchangeMoney>canExchangeMoney && exchangeMoney <= shortMoney) {
						score.setText(""+(int)(canExchangeMoney/info.card.scoreMoneyExchange));
					} else if (exchangeMoney<=scoreMaxMoney && exchangeMoney<=canExchangeMoney && exchangeMoney > shortMoney) {
						score.setText(""+(int)(shortMoney/info.card.scoreMoneyExchange));
					} else if (exchangeMoney<=scoreMaxMoney && exchangeMoney>canExchangeMoney && exchangeMoney > shortMoney) {
						if (canExchangeMoney < shortMoney) {
							score.setText(""+(int)(canExchangeMoney/info.card.scoreMoneyExchange));
						}else {
							score.setText(""+(int)(shortMoney/info.card.scoreMoneyExchange));
						}
					} else if (exchangeMoney>scoreMaxMoney && exchangeMoney>canExchangeMoney && exchangeMoney <= shortMoney) {
						if (scoreMaxMoney < canExchangeMoney) {
							score.setText(""+(int)(scoreMaxMoney/info.card.scoreMoneyExchange));
						}else {
							score.setText(""+(int)(canExchangeMoney/info.card.scoreMoneyExchange));
						}
					} else if (exchangeMoney>scoreMaxMoney && exchangeMoney<=canExchangeMoney && exchangeMoney > shortMoney) {
						if (scoreMaxMoney < shortMoney) {
							score.setText(""+(int)(scoreMaxMoney/info.card.scoreMoneyExchange));
						}else {
							score.setText(""+(int)(shortMoney/info.card.scoreMoneyExchange));
						}
					}
					info.setSpan(score);
				}
				
			}
		};
	
		cashWatcher = new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				Log.e("Test", "结果:"+s.toString());
				if (s.length() > 1 && s.charAt(1) != '.'
						&& s.charAt(0) == '0'){
					cash.setText(s.toString().substring(1));
					info.setSpan(cash);
					return;
				}
				if (s.length() < 1) {
					cash.setText("0");
					info.setSpan(cash);
					return;
				}
				if (".".equals(s.toString())) {
					cash.setText("0.");
					info.setSpan(cash);
					return;
				}

				double dismoney = Double.parseDouble(needPay.getText().toString());				
				double hasPayMoney = Double.parseDouble(s.toString())+Double.parseDouble(money.getText().toString())+(info.card.isScorePay?Integer.parseInt(score.getText().toString())/info.card.scoreMoneyExchange:0);
				double countResult = hasPayMoney - dismoney;
				payMoney.setText(info.subZeroAndDot(""+Math.round(100*hasPayMoney)/100.0)+"元");
				if (countResult>0.0000001) {
					result.setText("找零"+info.subZeroAndDot(Math.round(100*countResult)/100.0+"")+"元");
				} else if (Math.abs(countResult)<0.0000001) {
					result.setText("结清");
				} else {
					result.setText("少付"+info.subZeroAndDot(Math.round(-100*countResult)/100.0+"")+"元");
				}
			}
		};
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnTouchListener#onTouch(android.view.View,
	 * android.view.MotionEvent)
	 */
	@Override
	public boolean onTouch(View view, MotionEvent event) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.searchbtn:
			if (event.getAction() == MotionEvent.ACTION_UP) {
				searchCardNum.setBackgroundColor(Color.TRANSPARENT);
			} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
				searchCardNum.setBackgroundColor(Color.rgb(51, 102, 153));
			}
			break;
		default:

			break;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		dismoney = needmoney*info.storeDiscount*info.card.Discount;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.searchbtn:
			Intent intent = new Intent(this, ReadMemberInfoActivity.class);
			startActivityForResult(intent, 2);
			Toast.makeText(this, "读取卡号", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btnOK:			
			if (info.stopFlag) {
				Toast.makeText(this, "网络未连接，请确保您的网络已连接！", Toast.LENGTH_SHORT)
						.show();
			} else {
				if (!info.isExist(info.card.cardNum)) {
					Toast.makeText(this, "请先读取会员卡信息！", Toast.LENGTH_SHORT).show();
					return;
				} else if ("0".equals(needPay.getText().toString())) {
					Toast.makeText(this, "请输入应付金额！", Toast.LENGTH_SHORT).show();
					return;
				} else if (info.isPayNeedPassword && "".equals(password.getText().toString()) && (!"0".equals(info.subZeroAndDot(money.getText().toString())) || !"0".equals(score.getText().toString()))) {
					Toast.makeText(this, "请输入会员密码！", Toast.LENGTH_SHORT).show();
					
					return;
				}
				new Thread(this).start();
			}
			break;
//		case R.id.password:
//			if (Math.abs(Double.parseDouble(money.getText().toString()))>0.0000001 || !score.getText().toString().equals("0")) {
//				password.setFocusable(true);
//				Log.d("1", "1111");
////				password.setEnabled(true);
//			} else {
//				password.setFocusable(false);
//				Log.d("1", "2222");
//			}
//			break;
		default:
			break;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onActivityResult(int, int,
	 * android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 2:
			if (resultCode == Activity.RESULT_OK) {
				cardNum.setText(info.card.cardNum);
				ownmoney.setText(info.subZeroAndDot(info.card.money+"") + "元");
				ownscore.setText(info.card.score + "枚");
				needPay.setEnabled(true);		
				money.setEnabled(true);
				score.setEnabled(true);			
				cash.setEnabled(true);
				password.setText("");
			}
			break;
		default:
			break;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.view.View.OnFocusChangeListener#onFocusChange(android.view.View,
	 * boolean)
	 */
	@Override
	public void onFocusChange(View view, boolean hasFocus) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.needpay:
			if (hasFocus) {
				needPay.addTextChangedListener(needPayWatcher);
			} else {
				needPay.removeTextChangedListener(needPayWatcher);
			}
			break;

		case R.id.money:
			if (hasFocus) {
				money.addTextChangedListener(moneyWatcher);
			} else {
				money.removeTextChangedListener(moneyWatcher);
			}
			break;
		case R.id.score:
			if (hasFocus) {
				score.addTextChangedListener(scoreWatcher);
			} else {				
				score.removeTextChangedListener(scoreWatcher);
			}
			break;
		case R.id.cash:
			if (hasFocus) {
				cash.addTextChangedListener(cashWatcher);
			} else {
				cash.removeTextChangedListener(cashWatcher);
			}
			break;
		default:
			break;
		}
	}

	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			send();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void updata() {
		needPay.setText("0");
		payMoney.setText("0元");
		result.setText("");
		money.setText("0");
		score.setText("0");
		scoreMoney.setText("0元");
		cash.setText("0");
		password.setText("");
//		password.setEnabled(false);		
		new Thread(){
			@Override
			public void run() {
				try {
					info.updata();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};
		}.start();
		Intent intent = getIntent();						 
		/* 将数据打包到intent Bundle 的过程略 */
		setResult(RESULT_OK, intent); //这理有2个参数(int resultCode, Intent intent)															 
		finish();		
	}
	/**
	 * 
	 */
	private void send()  throws Exception{
		// TODO Auto-generated method stub
				String Url = "http://bi.bi-uc.com/BI.api";
				/*String Body = "Action=PDA-充值&商家=" + info.employID + "&店面="
						+ info.shopID + "&操作员=" + info.userID + "&卡号=" + cardNumString
						+ "&充值金额=" + moneyString;
				Log.d("data", Body);*/
				HttpPost httpRequest = new HttpPost(Url);
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				NameValuePair pair1 = new BasicNameValuePair("Action", "PDA-快速结账");
				NameValuePair pair2 = new BasicNameValuePair("商家", info.employID);				
				NameValuePair pair3 = new BasicNameValuePair("卡号", info.card.cardNum);
				NameValuePair pair4 = new BasicNameValuePair("店面", info.shopID);
				String s = needPay.getText().toString();
				/*int i = 2;
				if (!s.matches( "^\\-?([1-9]\\d*|0)(\\.\\d{0," + i + "})?$" )) {
					s = s.substring( 0, s.indexOf('.') + 3 );
				}*/
				NameValuePair pair5 = new BasicNameValuePair("应付金额", s);
				StringBuffer sb = new StringBuffer(payMoney.getText());
				NameValuePair pair6 = new BasicNameValuePair("实付金额", sb.deleteCharAt(sb.length()-1).toString());
				NameValuePair pair7 = new BasicNameValuePair("操作员", info.userID);
				NameValuePair pair8 = new BasicNameValuePair("储值支付", money.getText().toString());
				NameValuePair pair9 = new BasicNameValuePair("积分支付", score.getText().toString());
				sb = new StringBuffer(scoreMoney.getText());
//				Log.d("积分抵现", sb.deleteCharAt(sb.length()-1).toString());
				NameValuePair pair10 = new BasicNameValuePair("积分抵现", sb.deleteCharAt(sb.length()-1).toString());
				NameValuePair pair11 = new BasicNameValuePair("现金支付", cash.getText().toString());
				
				NameValuePair pair12 = new BasicNameValuePair("密码", password.getText().toString());
				params.add(pair12);
				Log.d("data", "应付金额:"+needPay.getText().toString()+"实付金额:"+pair6.getValue()
						+"储值支付:"+pair8.getValue()+"积分支付:"+pair9.getValue()
						+"积分抵现:"+pair10.getValue()+"现金支付:"+pair11.getValue()+"密码:"+pair12.getValue());
				params.add(pair1);
				params.add(pair2);
				params.add(pair3);
				params.add(pair4);
				params.add(pair5);
				params.add(pair6);
				params.add(pair7);
				params.add(pair8);
				params.add(pair9);
				params.add(pair10);
				params.add(pair11);
				HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
				httpRequest.setEntity(entity);
				DefaultHttpClient client = new DefaultHttpClient();
				HttpResponse response = client.execute(httpRequest);
				if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					/*InputStream in = response.getEntity().getContent();
					BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));  
					StringBuffer responseMessage = new StringBuffer();
					int charCount = -1;  
					   while ((charCount = br.read()) != -1) {  
						    responseMessage.append((char) charCount);  
						   }  
					   Log.d("tag", responseMessage.toString());*/
					InputStream in = response.getEntity().getContent();// 接收服务器的数据
					XmlPullParserFactory pullParserFactory = XmlPullParserFactory
							.newInstance();    // 获取XmlPullParser的实例					
					final XmlPullParser parser = pullParserFactory.newPullParser();
					InputStreamReader responseReader = new InputStreamReader(in,
							"UTF-8");
					parser.setInput(responseReader);
					parser.next();
					int eventType = parser.getEventType();
					if (eventType != XmlPullParser.END_DOCUMENT) {
						if (eventType == XmlPullParser.START_TAG
								&& parser.getName().equals("XML")) { // String result = parser.getAttributeValue(0);
							String result = parser.getAttributeValue(null, "EXEC");
							if (result.equals("True")) {
								info.card.money = info.card.money - Double.parseDouble(money.getText().toString());					
								info.card.score = info.card.score - Integer.parseInt(score.getText().toString());
								runOnUiThread(new Runnable() {
									@Override
									public void run() {
//										Intent intent = getIntent();						 
										/* 将数据打包到intent Bundle 的过程略 */
//										setResult(RESULT_OK, intent); //这理有2个参数(int resultCode, Intent intent)															 
//										finish();
										ownmoney.setText(info.subZeroAndDot(info.card.money+"") + "元");
										ownscore.setText(info.card.score + "枚");
//										Toast.makeText(QuickpayActivity.this, "快速结账成功！", Toast.LENGTH_SHORT).show();
										Log.d("data", "快速结账成功！");
										updata();
									}
								});
								
							} else {								
								final String error = parser.getAttributeValue(null, "错误消息");
								Log.e("data",error);
								if (error.contains("您的可用短信数量为")) {
									runOnUiThread(new Runnable() {

										@Override
										public void run() {
											// TODO Auto-generated method stub
											Toast.makeText(QuickpayActivity.this, error,
													Toast.LENGTH_SHORT).show();
											updata();
										}
										
									});
									
								} else {
									runOnUiThread(new Runnable() {

										@Override
										public void run() {
											// TODO Auto-generated method stub
											Toast.makeText(QuickpayActivity.this, "快速结账失败！"+error,
													Toast.LENGTH_SHORT).show();
										}
									});
								}
								
							}
						}
					}
					in.close();
				
					responseReader.close();
				}
				else
				{
					runOnUiThread(new Runnable() {				
						@Override
						public void run() {
							Toast.makeText(QuickpayActivity.this, "连接超时，请确保网络连接正常！", Toast.LENGTH_SHORT).show();
							}
					});	
				}		
	}
}
